/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef SHARED_DB_DATABASEIMPL_H_
#define SHARED_DB_DATABASEIMPL_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstdarg>
#include "Shared/Log/Log.h"
#include "Shared/Util/String.h"
#include "Shared/DB/Database.h"
#include "Shared/DB/SqlDelayThread.h"


#define ASYNC_QUERY_BODY(sql) if (!sql || !_resultQueue.get()) return false;

#define ASYNC_PQUERY_BODY(format, szQuery)                                                          \
    if(!format) { return false; }                                                                   \
    char szQuery[Database::MAX_QUERY_LENGTH];                                                       \
    std::va_list ap;                                                                                \
    va_start(ap, format);                                                                           \
    int res = Util::vsnprintf( szQuery, Database::MAX_QUERY_LENGTH, format, ap );                   \
    va_end(ap);                                                                                     \
    if(res == -1)                                                                                   \
    {                                                                                               \
        sharedLog.outDbError("SQL Query truncated (and not execute) for format: %s",format);        \
        return false;                                                                               \
    }                                                                                               \


namespace Shared {
namespace DB {

    template<class Class, typename ParamType1>
    bool Database::asyncQuery(Class* object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char* sql)
    {
        ASYNC_QUERY_BODY(sql);
        return _delayThread->delay(new SqlQuery(sql, new QueryCallback<Class, ParamType1>(object, method, (QueryResult*)NULL, param1), _resultQueue.get()));
    }

    template<class Class, typename ParamType1>
    bool Database::asyncPQuery(Class* object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char* format, ...)
    {
        ASYNC_PQUERY_BODY(format, szQuery);
        return asyncQuery(object, method, param1, szQuery);
    }

}}

#endif // SHARED_DB_DATABASEIMPL_H_
